package com.ruoyi.system.service.impl;

import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.Banner;
import com.ruoyi.system.mapper.BannerMapper;
import com.ruoyi.system.service.IBannerService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.poi.xssf.usermodel.TextHorizontalOverflow;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.rmi.ServerException;
import java.util.List;

/**
 * bannerService业务层处理
 *
 * @author 杨杰
 * @date 2024-03-04
 */
@Service
public class BannerServiceImpl implements IBannerService {
    @Autowired
    private BannerMapper bannerMapper;

    /**
     * 查询banner
     *
     * @param bannerId banner主键
     * @return banner
     */
    @Override
    public Banner selectBannerByBannerId(Long bannerId) {
        return bannerMapper.selectBannerByBannerId(bannerId);
    }

    /**
     * 查询banner列表
     *
     * @param banner banner
     * @return banner
     */
    @Override
    public List<Banner> selectBannerList(Banner banner) {
        return bannerMapper.selectBannerList(banner);
    }

    /**
     * 新增banner
     *
     * @param banner banner
     * @return 结果
     */
    @Override
    public int insertBanner(Banner banner) throws ServerException {
        List<String> bannerNames = bannerMapper.selectByBannerName(banner.getBannerName(), null);
        if (CollectionUtils.isNotEmpty(bannerNames)) {
            throw new ServerException("banner名:" + banner.getBannerName() + "已存在");
        }
        banner.setCreateBy(String.valueOf(SecurityUtils.getUserId()));
        banner.setUpdateBy(String.valueOf(SecurityUtils.getUserId()));
        banner.setCreateTime(DateUtils.getNowDate());
        banner.setUpdateTime(DateUtils.getNowDate());
        banner.setIsTopped("0");
        return bannerMapper.insertBanner(banner);
    }

    /**
     * 修改banner
     *
     * @param banner banner
     * @return 结果
     */
    @Override
    public int updateBanner(Banner banner) throws ServerException {
        List<String> bannerNames = bannerMapper.selectByBannerName(banner.getBannerName(), banner.getBannerId());
        if (CollectionUtils.isNotEmpty(bannerNames)) {
            throw new ServerException("banner名:" + banner.getBannerName() + "已存在");
        }
        banner.setUpdateBy(String.valueOf(SecurityUtils.getUserId()));
        banner.setUpdateTime(DateUtils.getNowDate());
        return bannerMapper.updateBanner(banner);
    }

    /**
     * 批量删除banner
     *
     * @param bannerIds 需要删除的banner主键
     * @return 结果
     */
    @Override
    public int deleteBannerByBannerIds(String bannerIds) {
        return bannerMapper.deleteBannerByBannerIds(Convert.toStrArray(bannerIds));
    }

    /**
     * 删除banner信息
     *
     * @param bannerId banner主键
     * @return 结果
     */
    @Override
    public int deleteBannerByBannerId(Long bannerId) {
        return bannerMapper.deleteBannerByBannerId(bannerId);
    }

    /**
     * 置顶
     */
    @Override
    public int editState(Banner banner) {
        String isTopped = banner.getIsTopped();
        //置顶操作
        if ("1".equals(isTopped)) {
            //查询出当前类型下的最大置顶数据
            String maxTopped = bannerMapper.getMaxTopped(banner.getBannerId());
            int i = Integer.parseInt(maxTopped) + 1;
            banner.setIsTopped(String.valueOf(i));
        }
        banner.setUpdateBy(String.valueOf(SecurityUtils.getUserId()));
        banner.setUpdateTime(DateUtils.getNowDate());
        return bannerMapper.updateBanner(banner);
    }
}
